package il.ac.technion.wiki.impl.tests;

import static org.junit.Assert.assertTrue;
import il.ac.technion.wiki.api.IPageStripper;
import il.ac.technion.wiki.api.IWikiEngine;
import il.ac.technion.wiki.api.IWikiPage;
import il.ac.technion.wiki.api.IWikiStorage;
import il.ac.technion.wiki.api.WikiException;
import il.ac.technion.wiki.impl.LightWikiEngine;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class LightWikiEngineTests {
	
	IPageStripper ps;
	IWikiStorage ws;
	IWikiEngine wiki;
	
	public LightWikiEngineTests() {
		ps = new IPageStripper() {
			
			@Override
			public String strip(String rawContent) {
				return null;
			}
		}; 
	}
	
	@Before
	public void before() {
		
		 ws = new IWikiStorage() {};
		 
		 wiki = new LightWikiEngine(ps, ws);
		 
	 }

	 @After
	 public void after() {
	   wiki = null;
	   ws = null;
	   System.gc();
	 }

	 @Test public void addPageTest() {
		 try {
				wiki.addPage(new SimplePage("Simple Page", "simple author", "simple content", "en", 1));
			} catch (WikiException e) {	e.printStackTrace(); }

		 assertTrue(wiki.isTitleExist("Simple Page"));
	 }
	 
	 @Rule public ExpectedException expectedException = ExpectedException.none();
	 
	 @Test public void addExistingPage() {
	    IWikiPage p = new SimplePage("Simple Page", "simple author", "simple content", "en", 1);
		try {
				wiki.addPage(p);
		} catch (WikiException e) {	e.printStackTrace(); }
		
		expectedException.expect(WikiException.class);
//		wiki.addPage(p);		
	 }
	 
	 @Test public void removePage() {
		 IWikiPage p = new SimplePage("Simple Page", "simple author", "simple content", "en", 1);
		 try {	
			 wiki.addPage(p);
		 } catch (WikiException e) {	e.printStackTrace(); }
		 
		 try {
			 wiki.removePage("Simple Page");
		 } catch (WikiException e) { e.printStackTrace(); }
		 
		 
	 }
	 
	 @Test public void removeNotExsistingPage() {
		 expectedException.expect(WikiException.class);
		 // wiki.removePage("no such page");
	 }
	 
	 @Test public void modifyPage(){
		 
	 }
	 
	 
	 

}


final class SimplePage implements IWikiPage {

	private String title;
	private String author;
	private String content;
	private String language;
	private double version;
	

	public SimplePage(String title, String author, String content, String language, double version){
		this.title = title;
		this.author = author;
		this.content = content;
		this.language = language;
		this.version = version;
	}
	@Override
	public String title() {
		return title;
	}

	@Override
	public String author() {
		return author;
	}

	@Override
	public String language() {
		return language;
	}
	
	@Override
	public String content() {
		return content;
	}

	@Override
	public double version() {
		return version;
	}

	
}
